package cn.supermarket.system.service.impl;

import cn.supermarket.common.contant.CacheConstants;
import cn.supermarket.common.contant.UserConstants;
import cn.supermarket.common.entity.system.SysDictData;
import cn.supermarket.common.exception.ServiceException;
import cn.supermarket.common.utils.CaffeineUtils;
import cn.supermarket.common.utils.DictUtils;
import cn.supermarket.common.utils.StringUtils;
import cn.supermarket.system.mapper.SysDictDataMapper;
import cn.supermarket.system.dto.SysDictTypeDTO;
import cn.supermarket.system.service.SysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.supermarket.common.entity.system.SysDictType;
import cn.supermarket.system.service.SysDictTypeService;
import cn.supermarket.system.mapper.SysDictTypeMapper;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>
    implements SysDictTypeService{
    @Autowired
    private LoadingCache dictCache;

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init()
    {
        loadingDictCache();
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictTypeParam 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeList(SysDictTypeDTO dictTypeParam)
    {
        return this.baseMapper.selectDictTypeList(dictTypeParam);
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache()
    {
        List<SysDictType> sysDictTypeList=this.list();
        List<SysDictData> sysDictDataList=sysDictDataService.list();
        for (SysDictType sysDictType:sysDictTypeList){
            CaffeineUtils.put(dictCache, getCacheKey(sysDictType.getDictType()),
                    sysDictDataList.stream().filter(s->s.getDictType().equals(sysDictType.getDictType())).collect(Collectors.toList()));
        }
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private static String getCacheKey(String configKey)
    {
        return CacheConstants.SYS_DICT_KEY + configKey;
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public boolean checkDictTypeUnique(SysDictType dict)
    {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        SysDictType dictType = this.baseMapper.checkDictTypeUnique(dict.getDictType());
        if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 批量删除字典类型信息
     *
     * @param dictIds 需要删除的字典ID
     */
    @Override
    public void deleteDictTypeByIds(Long[] dictIds)
    {
        for (Long dictId : dictIds)
        {
            SysDictType dictType = this.baseMapper.selectById(dictId);
            if (sysDictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
            this.baseMapper.deleteById(dictId);
            CaffeineUtils.remove(dictCache, DictUtils.getKey(dictType.getDictType()));
        }
    }

    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache()
    {
        clearDictCache();
        loadingDictCache();
    }

    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache()
    {
        CaffeineUtils.clear(dictCache);
    }
}




